home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / DefaultSingleSelectionModel.java < prev    next >
Text File  |  1998-06-30  |  3KB  |  117 lines

  1. /*
  2.  * @(#)DefaultSingleSelectionModel.java    1.15 98/02/02
  3.  *
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  *
  19.  */
  20.  
  21. package com.sun.java.swing;
  22.  
  23. import com.sun.java.swing.event.*;
  24. import java.io.Serializable;
  25.  
  26. /**
  27.  * A generic implementation of SingleSelectionModel.
  28.  * <p>
  29.  * Warning: serialized objects of this class will not be compatible with
  30.  * future swing releases.  The current serialization support is appropriate 
  31.  * for short term storage or RMI between Swing1.0 applications.  It will
  32.  * not be possible to load serialized Swing1.0 objects with future releases
  33.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  34.  * baseline for the serialized form of Swing objects.
  35.  *
  36.  * @version 1.15 02/02/98
  37.  * @author Dave Moore
  38.  */
  39. public class DefaultSingleSelectionModel implements SingleSelectionModel, 
  40. Serializable {
  41.     /* Only one ModelChangeEvent is needed per model instance since the
  42.      * event's only (read-only) state is the source property.  The source
  43.      * of events generated here is always "this".
  44.      */
  45.     protected transient ChangeEvent changeEvent = null;
  46.     protected EventListenerList listenerList = new EventListenerList();
  47.  
  48.     private int index = -1;
  49.  
  50.     public int getSelectedIndex() {
  51.         return index;
  52.     }
  53.  
  54.     public void setSelectedIndex(int index) {
  55.         if (this.index != index) {
  56.             this.index = index;
  57.         fireStateChanged();
  58.         }
  59.     }
  60.  
  61.     public void clearSelection() {
  62.         setSelectedIndex(-1);
  63.     }
  64.  
  65.     public boolean isSelected() {
  66.     boolean ret = false;
  67.     if (getSelectedIndex() != -1) {
  68.         ret = true;
  69.     }
  70.     return ret;
  71.     }
  72.  
  73.     /**
  74.      * Adds a ChangeListener to the button.
  75.      */
  76.     public void addChangeListener(ChangeListener l) {
  77.     listenerList.add(ChangeListener.class, l);
  78.     }
  79.     
  80.     /**
  81.      * Removes a ChangeListener from the button.
  82.      */
  83.     public void removeChangeListener(ChangeListener l) {
  84.     listenerList.remove(ChangeListener.class, l);
  85.     }
  86.     /*
  87.      * Notify all listeners that have registered interest for
  88.      * notification on this event type.  The event instance 
  89.      * is lazily created using the parameters passed into 
  90.      * the fire method.
  91.      * @see EventListenerList
  92.      */
  93.     protected void fireStateChanged() {
  94.     // Guaranteed to return a non-null array
  95.     Object[] listeners = listenerList.getListenerList();
  96.     // Process the listeners last to first, notifying
  97.     // those that are interested in this event
  98.     for (int i = listeners.length-2; i>=0; i-=2) {
  99.         if (listeners[i]==ChangeListener.class) {
  100.         // Lazily create the event:
  101.         if (changeEvent == null)
  102.             changeEvent = new ChangeEvent(this);
  103.         ((ChangeListener)listeners[i+1]).stateChanged(changeEvent);
  104.         }           
  105.     }
  106.     }    
  107. }
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.